package com.geccocrawler.gecco.demo.cangzhou;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.demo.qinhuangdao.Item;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.DeriveSchedulerContext;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: weixiaohu
 * Date: 2019/6/3
 * Time: 15:05
 */
@PipelineName("cangzhouPipeline")
public class ProcessPipeline implements Pipeline<Results> {
    private List allData = Lists.newCopyOnWriteArrayList();
    @Override
    public void process(Results results) {
        HttpRequest currRequest = results.getRequest();
        String url = currRequest.getUrl();
        int totalPage = results.getTotalPages();
        int currPage = results.getPageNo();
        int nextPage = currPage + 1;
        allData.addAll(results.getList());
        if(nextPage <= totalPage) {
            String nextUrl = StringUtils.replaceOnce(url, "pageNo=" + currPage, "pageNo=" + nextPage);
            DeriveSchedulerContext.into(currRequest.subRequest(nextUrl));
        }else{
            genExcel(allData);
        }

    }

    private void genExcel(List<Item> rows){
        ExcelWriter writer = ExcelUtil.getWriter("d:/spider/定点医疗机构.xlsx");
//        writer.addHeaderAlias("name", "名称");
//        writer.addHeaderAlias("code", "编码");
//        writer.addHeaderAlias("address", "地址");
//        writer.addHeaderAlias("level", "级别名称");
//        writer.addHeaderAlias("ownerRegion", "所属经办机构");
        writer.write(rows, true);
        writer.close();
    }
}
